import axios from "axios";
import {
	createUniAppAxiosAdapter
} from '@uni-helper/axios-adapter'
const service = axios.create({
	// http://120.26.166.190:8091/api
	baseURL: 'http://120.26.166.190:8091/api',
	timeout: 5000,
	adapter: createUniAppAxiosAdapter(),
});

//请求拦截器     在请求头中添加jwt
service.interceptors.request.use(
	(config) => {
		//拿到浏览器存储的token
		config.headers.Authorization = uni.getStorageSync('Authorization');
		// localStorage.getItem("Authorization");
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

//响应拦截器
service.interceptors.response.use(
	(response) => {
		//将data值取出
		const data = response.data;

		//判断状态  0为成功 -1失败
		if (data.code === 0) {
			//成功返回数据
			return data;
		} else {
			//提示错误信息
			// ElMessage.error(data.message);
			uni.showToast({
				title: data.message,
				duration: 1000
			});;
			return Promise.reject(new Error(data.message));
		}
	},
	(error) => {
		//如果有错误信息，那么就打印出来
		error.response && uni.showToast({
			title: error.response.data,
			duration: 10000
		});
		console.log(error.response.data);
		// return Promise.reject(new Error(error.response.data));
	}
);
export default service;